package com.wuzao.optionalTest;

import cn.hutool.core.util.StrUtil;
import lombok.Data;
import org.junit.Test;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import javax.swing.text.html.Option;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class OptionalTest {
    @Data
    class Person{
        String id;
        String name;
        List<String> girls;
    }

    @Test
    public void test1(){    
        Person person = null;
        // ifPresent 用于对过滤出的数据如果存在。如果经过过滤条件后，有数据的话就可以进行修改。
        Optional.ofNullable(person).ifPresent(i->{
            i.setId("1");
        });
        System.out.println(person);



        Person person1 = new Person();
        person1.setGirls(new ArrayList<>());
        person1.getGirls().add("1");
        Optional.ofNullable(person1).ifPresent(i->{
            i.setId("1");
        });

        System.out.println(person1);


    }

    @Test
    public void testOrElseThrow(){
        // 不会抛异常
        Person person1 = new Person();
        person1.setId("1");
        Person person3 = Optional.ofNullable(person1).orElseThrow(()->new NullPointerException("person is null"));
        System.out.println(person3);

        // 会抛异常
//        Person person = null;
//        Person person2 = Optional.ofNullable(person).orElseThrow(()->new NullPointerException("person is null"));
//        System.out.println(person2);

        List<Person> personList = new ArrayList<>();
        personList.add(person1);
        String str = "2";
        Person person4 = personList.stream().filter(i-> StrUtil.equals(i.getId(), "2"))
                .findAny().orElseThrow(()-> new  NullPointerException("cannot find id = " + str + " person"));
        System.out.println(person4);
    }

    @Test
    public void testOrElse(){
        List<Person> personList = null;
        List<Person> personList1 = Optional.ofNullable(personList).orElse(new ArrayList<>());
        System.out.println(personList1);

        System.out.println(personList1.stream().map(Person::getName).collect(Collectors.toList()));

        System.out.println(personList.stream().map(Person::getName).collect(Collectors.toList()));

    }


    @Test
    public void test6(){
        // 创建 SpEL 表达式解析器
        ExpressionParser parser = new SpelExpressionParser();

        // 要解析的字符串
        String expressionString = "${option=='submit-next'}";

        // 创建 SpEL 上下文
        StandardEvaluationContext context = new StandardEvaluationContext();

        // 设置变量
        context.setVariable("option", "submit-next");

        // 替换表达式中的变量并解析为 SpEL 表达式
        String parsedExpressionString = expressionString
                .replace("${", "#").replace("}", "");
        Expression expression = parser.parseExpression(parsedExpressionString);

        // 在上下文中求值
        boolean result = expression.getValue(context, Boolean.class);

        // 输出结果
        System.out.println(result); // 输出: true

    }


}
